package com.sz.controller;

import com.sz.entity.Result;
import com.sz.exception.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class MyExceptionAdvice {

    private static final Logger log = LoggerFactory.getLogger(MyExceptionAdvice.class);

    @ExceptionHandler(MyException.class)
    public Result handlerMyException(MyException e){
        log.info("业务异常");
        return new Result(false,e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){
        log.error("发生未知异常",e);
        return new Result(false,"发生未知异常请联系管理员");
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAccessDeniedException(AccessDeniedException e){
        // 没有权限
        return new Result(false, "没有权限");
    }

}
